package com.blacksee.bluetoothpoker.network.messages;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import com.blacksee.bluetoothpoker.logic.Card;
import com.blacksee.bluetoothpoker.logic.net.mypapit.java.StringTokenizer;

public class StartGameMessage implements NetMessage {

	private Hashtable players;
	
	private Vector cards;
	
	public StartGameMessage(){
	}
	
	public StartGameMessage(Hashtable players, Vector cards){
		this.players = players; 
		this.cards = cards;
	}
	
	public int getSenderID() {
		return 0;
	}
	
	public Hashtable getPlayers() {
		return players;
	}
	
	public Vector getCards() {
		return cards;
	}
	
	public void parseParameters(String message) {
		// gets the list of the players in the game starting
		StringTokenizer tok = new StringTokenizer(message);
		String messageHead = tok.nextToken("*");
		
		String playersStr = tok.nextToken("*");
		
		StringTokenizer tok2 = new StringTokenizer(playersStr,",");
		String player = "";
		players = new Hashtable();
		while (tok2.hasNext())
		{
			player = tok2.nextToken(",");
			
			int sep = player.indexOf(":");
			
			String playerID =  player.substring(0,sep);
			String playerName = player.substring(sep+1);
			
			players.put(playerID, playerName);
				
		}
		
		String cardsStr = tok.nextToken("*");
		
		StringTokenizer tok3 = new StringTokenizer(playersStr,",");
		Card card = null;
		cards = new Vector();
		while (tok3.hasNext()) {
			card = new Card(tok3.nextToken(","));
			cards.addElement(card);
		}
	}

	public String GetMessageString() {
		String text = "START_GAME";
		
		String playersText = "*";
		Enumeration e = players.keys();
		while( e. hasMoreElements() ){
			Integer id = (Integer)e.nextElement();
			String name = (String)players.get(id);
			
			if (playersText != "*" && playersText != null)
				playersText += ",";
			
			playersText += id + ":" + name;
			
		}
		text += playersText;
		
		String cardsText = "*";
		if(cards != null){
			for (int i = 0; i < cards.size(); i++) {
				Card c = (Card)cards.elementAt(i);
				cardsText += c.toString();
				
				if (i < cards.size() - 1) cardsText += ",";
			}
		}
		text += cardsText;
		System.out.println("Start message: " + text);
		return text;
	}
	

}
